package com.googlecode.i78ba.login;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.googlecode.i78ba.util.ServiceException;

@Controller
@RequestMapping(value = "/login")
public class LoginController {
	private static Logger log = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private LoginService loginService;

	@RequestMapping(value = "/register")
	@ResponseBody
	public Map<String, Object> register(HttpServletRequest request) {
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			User user = new User();
			user.setName(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			user.setEmail(request.getParameter("email"));
			loginService.register(user);
			data.put("state", "success");
		} catch (Exception e) {
			data.put("state", "fail");
			log.error("", e);
		}
		return data;
	}
	
	@RequestMapping(value = "")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request){
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			User loginUser = new User();
			loginUser.setName(request.getParameter("login_username"));
			loginUser.setPassword(request.getParameter("login_password"));
			User user = loginService.login(loginUser);
			request.getSession().setAttribute("user", user);
			data.put("state", "success");
		} catch (ServiceException e) {
			data.put("state", e.getMessage());
		} catch (Exception e) {
			data.put("state", "fail");
			log.error("",e);
		}
		return data;
	}
	
	@RequestMapping(value = "/loggedin_avatar")
	public void loggedinAvatar(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String size = request.getParameter("size");
			if(size==null){
				size = "small";
			}
			
			response.setHeader("Content-Type", "image");
			String webRootPath = request.getSession().getServletContext()
					.getRealPath("");
			String noavatarSmallpath = webRootPath + File.separator + "css"
					+ File.separator + "images" + File.separator
					+ "noavatar_" + size + ".gif";
			File file = new File(noavatarSmallpath);
			InputStream in = new BufferedInputStream(new FileInputStream(file));
			OutputStream out = response.getOutputStream();
			IOUtils.copy(in, out);
		} catch (IOException e) {
			log.error("",e);
		}
	}
}
